// 测试数据生成工具
const { getPlaylistSongs } = require('./config')

// 生成测试播放历史
function generateTestHistory() {
  const allSongs = []
  
  // 从所有歌单中收集歌曲
  for (let i = 1; i <= 6; i++) {
    const songs = getPlaylistSongs(i)
    allSongs.push(...songs)
  }
  
  // 随机选择一些歌曲作为播放历史
  const historyCount = Math.min(15, allSongs.length)
  const shuffled = allSongs.sort(() => 0.5 - Math.random())
  const selectedSongs = shuffled.slice(0, historyCount)
  
  return selectedSongs.map((song, index) => ({
    ...song,
    playTime: Date.now() - (index * 3600000), // 每小时播放一首
    playCount: Math.floor(Math.random() * 10) + 1
  }))
}

// 生成测试收藏列表
function generateTestFavorites() {
  const allSongs = []
  
  // 从所有歌单中收集歌曲
  for (let i = 1; i <= 6; i++) {
    const songs = getPlaylistSongs(i)
    allSongs.push(...songs)
  }
  
  // 随机选择一些歌曲作为收藏
  const favoriteCount = Math.min(8, allSongs.length)
  const shuffled = allSongs.sort(() => 0.5 - Math.random())
  const selectedSongs = shuffled.slice(0, favoriteCount)
  
  return selectedSongs.map((song, index) => ({
    ...song,
    addTime: Date.now() - (index * 86400000) // 每天收藏一首
  }))
}

// 生成测试用户歌单
function generateTestPlaylists() {
  return [
    {
      id: 'user_1',
      name: '我喜欢的音乐',
      coverImgUrl: '/assets/playlist-hot.svg',
      createTime: Date.now() - 30 * 86400000,
      songCount: 25,
      isDefault: true
    },
    {
      id: 'user_2', 
      name: '深夜电台',
      coverImgUrl: '/assets/playlist-classic.svg',
      createTime: Date.now() - 15 * 86400000,
      songCount: 12,
      isDefault: false
    },
    {
      id: 'user_3',
      name: '运动音乐',
      coverImgUrl: '/assets/playlist-pop.svg', 
      createTime: Date.now() - 7 * 86400000,
      songCount: 18,
      isDefault: false
    }
  ]
}

// 初始化测试数据
function initTestData() {
  const app = getApp()
  
  // 如果已经有数据，不重复初始化
  if (app.globalData.playHistory.length > 0) {
    return
  }
  
  // 生成测试数据
  const testHistory = generateTestHistory()
  const testFavorites = generateTestFavorites()
  const testPlaylists = generateTestPlaylists()
  
  // 保存到全局数据
  app.globalData.playHistory = testHistory
  app.globalData.favorites = testFavorites
  app.globalData.userPlaylists = testPlaylists
  
  // 保存到本地存储
  wx.setStorageSync('playHistory', testHistory)
  wx.setStorageSync('favorites', testFavorites)
  wx.setStorageSync('userPlaylists', testPlaylists)
  
  console.log('测试数据初始化完成')
}

// 清除所有测试数据
function clearTestData() {
  const app = getApp()
  
  app.globalData.playHistory = []
  app.globalData.favorites = []
  app.globalData.userPlaylists = []
  
  wx.removeStorageSync('playHistory')
  wx.removeStorageSync('favorites')
  wx.removeStorageSync('userPlaylists')
  
  console.log('测试数据已清除')
}

module.exports = {
  generateTestHistory,
  generateTestFavorites,
  generateTestPlaylists,
  initTestData,
  clearTestData
}